package com.simple.mq.rabbit;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 项目: mianshi-test
 * <p>
 * 功能描述: 处理连接
 * 消息处理中心服务 BrokerServer
 *
 * @author: WuChengXing
 * @create: 2021-04-22 23:52
 **/
public class BrokerServer implements Runnable {
    /**
     * 设置的监听端口
     */
    public static int SERVICE_PORT = 9999;

    private final Socket socket;

    /**
     * 初始化一个socket
     *
     * @param socket
     */
    public BrokerServer(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try (
                // 从socket中读取数据
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));
                // 写出数据
                PrintWriter out = new PrintWriter(socket.getOutputStream())
        ) {
            while (true) {
                String str = in.readLine();
                if (str == null) {
                    continue;
                }
                System.out.println("接收到原始数据：" + str);
                // CONSUME 表示要消费一条消息
                if (str.equals("CONSUME")) {
                    //从消息队列中消费一条消息
                    String message = Broker.consume();
                    out.println(message);
                    out.flush();
                } else if (str.contains("SEND:")) {
                    // 接受到的请求包含SEND:字符串 表示生产消息放到消息队列中
                    Broker.produce(str);
                } else {
                    System.out.println("原始数据:" + str + "没有遵循协议,不提供相关服务");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(SERVICE_PORT);
        while (true) {
            BrokerServer brokerServer = new BrokerServer(server.accept());
            new Thread(brokerServer).start();
        }
    }
}
